$(function(){
	
//	showAllPointOnMap();
	var time = 0;
	if(time == 0){
		showAllPointOnMap('init');
	}
	time = time + 20000;
	setInterval('showAllPointOnMap()',time);
});

/* 日期格式化 */
Date.prototype.Format = function(fmt) {
    var o = {
        "M+" : this.getMonth() + 1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
    for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1,
                    (RegExp.$1.length == 1) ? (o[k])
                            : (("00" + o[k])
                            .substr(("" + o[k]).length)));
    return fmt;
}

function showAllPointOnMap(type){
	var taskId = $("#taskId").val();
	var pagePath = $("#pagePath").val();
	var gisResourcePath = $("#gisResourcePath").val();
	var gisArr = new Array();
	var dataArr = new Array();
	var jsonObj = {};
	var url = pagePath +"/pig/cleanPipeLineTrack/initMapData.json";
	$().invoke(url,{"taskId":taskId},[function(m,r){
		var list = r.list;
		var mp = r.map.pig;
		var html ="";
		$("#pointDiv").html("");
		$.each(list,function(i,evalue){
			var lng = convertObj(evalue.lng);
			var lat = convertObj(evalue.lat);
			var code = convertObj(evalue.code);
			var _type = convertObj(evalue.type);
			var reference = convertObj(evalue.reference);
			var remark = convertObj(evalue.remark);
			var lineName = convertObj(evalue.lineName);
			var segmentName = convertObj(evalue.segmentName);
			var regionName = convertObj(evalue.regionName);
			var recorder = convertObj(evalue.recorder);
			var passTime = evalue.passTime;
			var customnumber = convertObj(evalue.customnumber);
			var recorder = convertObj(evalue.recorder);
			var state = evalue.state;
			var gisId = evalue.gisId;
			var gisStr = gisId + ";";
			var zuobiao = evalue.zuobiao;
			jsonObj = {
					"lng" : lng,
					"lat" : lat,
					"customnumber" : customnumber,
					"code" : code,
					"type" : _type,
					"reference" : reference,
					"remark" : remark,
					"lineName" : lineName,
					"segmentName" : segmentName,
					"regionName" : regionName,
					"recorder":recorder,
					"recorderTime":passTime,
					"state" : state
				};
			dataArr.push(jsonObj);
			var mil = 0;
			if(null != evalue.mileage && undefined != evalue.mileage){
				mil = evalue.mileage.substring(0,convertObj(evalue.mileage).indexOf(".")+4)
			}
			
			html += "<li class='zhou w80'></li>" +
					"<li class='sl'>" +
					"<i onclick='showPointOnMap("+lng+","+lat+");'><em class='e01'></em></i>" +
					"<span class='s01-1'>"+convertObj(evalue.customnumber)+"</span>" +
					"<span style='width:150px;' class='s02-2'>距离:"+mil+" m</span>" ;
			if(convertObjDate(passTime) != ""){
				html += "<em class='ren active'></em>";
				gisArr.push(jsonObj);
			}else{
				html += "<em class='ren '></em>";
			}
			html +="</li>";
		});
		$("#pointDiv").html(html);
		
		//收发球桶相关信息
		loadPiggingInfo(dataArr,gisArr,type,mp);
		if('init' == type){
			getForm(gisResourcePath + "/bMapTrackRealtime.jsp",dataArr);
		}else{
			document.getElementById("frameId").contentWindow.cleanPipeRealtime(gisArr);
		}
		
		//调整最新通过的跟踪点在视野内。通过对整个跟踪条html中通过的跟踪点和未通过的跟踪点的样式进行判断
		//1 获取最新通过的点的位置
//		var pd = $("#pointDiv").find(".sl");
//		anchorPoint(pd);
		
	}]);
}


function showPointOnMap(lng,lat){
	document.getElementById("frameId").contentWindow.locationMarker(lng,lat);
}


function convertObj(obj){
    return null==obj || "" == $.trim(obj) || undefined == obj ? "" : obj;
}
function convertObjDate(obj){
    return null==obj || "" == $.trim(obj) || undefined == obj ? "" : new Date(obj).Format("yyyy-MM-dd hh:mm:ss");
}

function anchorPoint(obj){
	var li = obj;
	var em = obj.children("em");
	var count = 0;
	for (var i = 0; i < em.length; i++) {
		if(em.hasClass("active")){
			count = i++;
		}
	}
	if((count-1) >= 0){
		location.href = "#"+em.eq(count-1).parent().children("span").eq(0).html();
	}
}

function loadPiggingInfo(obj1,obj2,type,mp){
	var frompiginfo ="";
	var topiginfo="";
	if('init'==type){
		frompiginfo = convertObj($("#frompiginfo").val());
		topiginfo = convertObj($("#topiginfo").val());
	}else{
		frompiginfo = mp.frompiginfo;
		topiginfo = mp.topiginfo;
	}
	var ssClass = $(".ss .start");
	
	var frompiginfoArr = frompiginfo.split("#%#");
	ssClass.eq(0).children("i").attr("onclick","showPointOnMap('"+convertObj(frompiginfoArr[0])+"','"+convertObj(frompiginfoArr[1])+"');");
	var sendBallTime = convertObj(frompiginfoArr[17]);
	var sendState = "";
	if("" == sendBallTime){
		ssClass.eq(0).children("em").addClass("ren");
		sendState = "N";
	}else{
		ssClass.eq(0).children("em").addClass("ren active");
		sendState = "Y";
	}
	var fromPigJson = {
			'lineName':convertObj($("#lineName").val()),
			'segmentName':convertObj($("#segmentName").val()),
			//发球筒X坐标
			'lng' : convertObj(frompiginfoArr[0]),
			//发球筒Y坐标
			'lat' : convertObj(frompiginfoArr[1]),
			//发球筒编号
			'customnumber': convertObj(frompiginfoArr[16]),
			//发球筒统一编号
			'code' : convertObj(frompiginfoArr[15]),
			//发球筒所属场站阀室
			'regionName' : convertObj(frompiginfoArr[3]),
			//发球筒压力等级
			'press' : convertObj(frompiginfoArr[4]),
			//发球筒的小球桶长度
			'SBlength' : convertObj(frompiginfoArr[5]),
			//发球筒的小球筒直径
			'SBdiameter' : convertObj(frompiginfoArr[6]),
			//发球筒的小球筒壁厚
			'SBthick' : convertObj(frompiginfoArr[7]),
			//发球筒的大球桶长度
			'BBlength' : convertObj(frompiginfoArr[8]),
			//发球筒的大球筒直径
			'BBdiameter' : convertObj(frompiginfoArr[9]),
			//发球筒的大球桶壁厚
			'BBthick' : convertObj(frompiginfoArr[10]),
			//发球筒的生产厂商 
			'factory' : convertObj(frompiginfoArr[2]),
			//发球筒的生产日期
			'factoryTime' : frompiginfoArr[11],
			//发球筒的记录人
			'recorder' : convertObj(frompiginfoArr[18]),
			//发球筒的记录时间
			//'recordTime' : convertObjDate(frompiginfoArr[13]),
			'recorderTime' : sendBallTime,
			//发球筒备注
			'remark':convertObj(frompiginfoArr[14]),
			
			'pointType' : 'fromGisPig',
			'sendBallTime':sendBallTime,
			'state' :sendState
	}
	
	var topiginfoArr = topiginfo.split("#%#");
	ssClass.eq(1).children("i").attr("onclick","showPointOnMap('"+convertObj(topiginfoArr[0])+"','"+convertObj(topiginfoArr[1])+"');");
	var backBallTime = convertObj(topiginfoArr[17]);
	var backState = "";
	if("" == backBallTime){
		ssClass.eq(1).children("em").addClass("ren");
		backState = "N"
	}else{
		ssClass.eq(1).children("em").addClass("ren active");
		backState = "Y"
	}
	
	var toPigJson = {
			'lineName':convertObj($("#lineName").val()),
			'segmentName':convertObj($("#segmentName").val()),
			//收球筒X坐标
			'lng' : convertObj(topiginfoArr[0]),
			//收球筒Y坐标
			'lat' : convertObj(topiginfoArr[1]),
			//收球筒编号
			'customnumber': convertObj(frompiginfoArr[16]),
			//收球筒统一编号
			'code' : convertObj(topiginfoArr[15]),
			//收球筒所属场站阀室
			'regionName' : convertObj(topiginfoArr[3]),
			//收球筒压力等级
			'press' : convertObj(topiginfoArr[4]),
			//收球筒的小球桶长度
			'SBlength' : convertObj(topiginfoArr[5]),
			//收球筒的小球筒直径
			'SBdiameter' : convertObj(topiginfoArr[6]),
			//收球筒的小球筒壁厚
			'SBthick' : convertObj(topiginfoArr[7]),
			//收球筒的大球桶长度
			'BBlength' : convertObj(topiginfoArr[8]),
			//收球筒的大球筒直径
			'BBdiameter' : convertObj(topiginfoArr[9]),
			//收球筒的大球桶壁厚
			'BBthick' : convertObj(topiginfoArr[10]),
			//收球筒的生产厂商 
			'factory' : convertObj(topiginfoArr[2]),
			//收球筒的生产日期
			'factoryTime' : convertObj(topiginfoArr[11]),
			//收球筒的记录人
			'recorder' : convertObj(topiginfoArr[18]),
			//收球筒的记录时间
			//'recordTime' : convertObjDate(topiginfoArr[13]),
			'recorderTime' : backBallTime,
			//收球筒备注
			'remark':convertObj(topiginfoArr[14]),
			
			'pointType' : 'toGisPig',
			'backBallTime':backBallTime,
			'state' :backState
	}
	
	//发球筒：'pointType':fromGisPig  收球筒 'pointType':toGisPig
	obj1.push(fromPigJson);
	obj1.push(toPigJson);
	if("" != sendBallTime){
		obj2.push(fromPigJson);
	}
	if("" != backBallTime){
		obj2.push(toPigJson);
	}
}


/**
 * 加载地图调用的方法
 * @param url 地图绝对路径
 * @param ap
 */
function getForm(url,ap){
	var formObj = document.createElement("form");
	formObj.setAttribute("method","post");
	formObj.setAttribute("action",url);
	var inputObj = document.createElement("input");
	inputObj.setAttribute("type","hidden");
	inputObj.setAttribute("name","gisIdArr");
	var valueStr = "";
	//如浏览器不支持JSON对象，则引入json2.js
	/*例如：
	 * 
	 * <!--[if lte IE 7]>
		<script type="text/javascript" src="/js/json2.js"></script>
	<![endif]-->
	 * */
	valueStr = JSON.stringify(ap);
	inputObj.setAttribute("value",valueStr);	
//	console.log(valueStr);
	formObj.appendChild(inputObj);
//	document.body.appendChild(iframeObj);
	//dyMap为iframe的 Id
	document.getElementById('frameId').contentWindow.document.write(formObj.outerHTML);
	document.getElementById('frameId').contentWindow.document.getElementsByTagName("form")[0].submit();
}